package com.project.business.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.solon.plugins.pagination.Page;
import com.project.business.entity.UserEntity;
import com.project.business.service.UserEntityService;
import com.common.common.Response;
import com.project.core.jwt.AuditAware;
import org.noear.solon.annotation.*;

import java.util.List;

@Controller
@Mapping("/user")
public class UserResource {

    @Inject
    private UserEntityService userService;

    @Get
    @Mapping("/info")
    public UserEntity info() {
        Integer userId = AuditAware.userId();
        return userService.getOneById(userId);
    }

    @Get
    @Mapping("/list")
    public List<UserEntity> list() {
        return userService.lists();
    }

    @Get
    @Mapping("/page")
    public IPage<UserEntity> page(Integer page, Integer rows) {
        return userService.pages(page, rows);
    }

    @Post
    @Mapping("/register")
    public Response register(@Body UserEntity entity) {
        userService.add(entity);
        return Response.success();
    }

    @Put
    @Mapping("/edit")
    public Response edit(@Body UserEntity entity) {
        userService.edit(entity);
        return Response.success();
    }

    @Post
    @Mapping("/login")
    public String login(@Param("username") String username,
                        @Param("password") String password) {
        return userService.login(username, password);
    }

    @Post
    @Mapping("/logout")
    public Response logout() {
        userService.logout();
        return Response.success("退出登录成功！");
    }

}
